﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyTruongHoc_App.MyDataSet;
using QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters;

namespace QuanLyTruongHoc_App
{
    public partial class QLHocSinh : Form
    {
        private int state, pow;
        private MyControls.ChonLop frm_ChonLop;

        public QLHocSinh()
        {
            InitializeComponent();
            dg_DanhSach.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dg_DanhSach_DataBindingComplete);
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.Format = "dd/MM/yyyy";
            //grc_NgayNhapTruong.DefaultCellStyle = 
                grc_NgaySinh.DefaultCellStyle = style;

            dg_DanhSach.ContextMenuStrip = ctm_HocSinh;
            
        }

        private void checkUserPower()
        {
            pow = PublicLib.getUserPower(PConst.QUYEN_QL_HOC_SINH);
            // user co quyen view
            /*if (!((pow & PConst.POW_VIEW) > 0))
            {
                MessageBox.Show(MSG.MSG_KHONG_DU_QUYEN);
                this.Close();
            }*/
            // user co quyen them
            tsb_TaoMoi.Enabled = ((pow & PConst.POW_ADD) > 0);

            // user co quyen edit
            tsb_Edit.Enabled = ((pow & PConst.POW_EDIT) > 0);
            // user co delete
            dg_DanhSach.AllowUserToDeleteRows =
                tsb_Delete.Enabled = ((pow & PConst.POW_DEL) > 0);
        }

        private void QuanLyGiaoVien_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'qLTH.tbl_HocSinh' table. You can move, or remove it, as needed.
            loadCombo();
            refreshData();
        }

        private void loadCombo()
        {
            state = PConst.STATE_LOAD;
            // Nam Hoc
            MyDataSet.QLTHTableAdapters.tbl_NamHocTableAdapter namAdap = new tbl_NamHocTableAdapter();
            namAdap.Connection.ConnectionString = PublicLib.getStrConn();
            namAdap.FillByFlag(qLTH.tbl_NamHoc, PConst.FLAG_NONE);

            tscb_NamHoc.Items.Clear();
            for (int i = 0; i < qLTH.tbl_NamHoc.Rows.Count; i++)
            {
                tscb_NamHoc.Items.Add(qLTH.tbl_NamHoc.Rows[i][qLTH.tbl_NamHoc.TenColumn].ToString());
            }

            if (tscb_NamHoc.Items.Count > 0)
                tscb_NamHoc.SelectedIndex = tscb_NamHoc.Items.Count - 1;
            // ---------------

            MyDataSet.QLTHTableAdapters.tbl_LopHocTableAdapter lopAdap = new tbl_LopHocTableAdapter();
            lopAdap.Connection.ConnectionString = PublicLib.getStrConn();
            lopAdap.Fill(qLTH.tbl_LopHoc);

            tscb_Lop.Items.Clear();
            for (int i = 0; i < qLTH.tbl_LopHoc.Rows.Count; i++)
            {
                tscb_Lop.Items.Add(qLTH.tbl_LopHoc.Rows[i][qLTH.tbl_LopHoc.TenLopColumn].ToString());
            }

            tscb_Lop.Items.Add(MSG.MNU_ALL);
            // load flag type
            for (int i=0; i<PConst.HS_FLAG.Length; i++)
            {
                tscb_Flag_HS.Items.Add(PConst.HS_FLAG_NAME[i]);
            }

            tscb_Flag_HS.Items.Add(MSG.MNU_ALL);
            // --------------------
            state = PConst.STATE_LOADED;
        }

        private void refreshData()
        {
            if (this.IsDisposed) return;

            this.tbl_HocSinhTableAdapter.Connection.ConnectionString =
                PublicLib.getStrConn();
            int idNamHoc = (int)qLTH.tbl_NamHoc[tscb_NamHoc.SelectedIndex][qLTH.tbl_NamHoc.idColumn];
            if (tscb_Lop.SelectedIndex != -1 && tscb_Lop.SelectedIndex < tscb_Lop.Items.Count - 1)
            {
                // xem cac nam truoc
                int idLop = (int)qLTH.tbl_LopHoc[tscb_Lop.SelectedIndex][qLTH.tbl_LopHoc.idColumn];
                if (tscb_Flag_HS.SelectedIndex != -1)
                {
                    if (tscb_Flag_HS.SelectedIndex != tscb_Flag_HS.Items.Count - 1)
                        tbl_HocSinhTableAdapter.FillByLopCu(qLTH.tbl_HocSinh, idNamHoc, idLop, PConst.HS_FLAG[tscb_Flag_HS.SelectedIndex]);
                    else
                        tbl_HocSinhTableAdapter.FillByLopCuAll(qLTH.tbl_HocSinh, idNamHoc, idLop);
                }
                else
                {
                    if (tscb_NamHoc.SelectedIndex == tscb_NamHoc.Items.Count - 1) 
                        tbl_HocSinhTableAdapter.FillByLopCu(qLTH.tbl_HocSinh, idNamHoc, idLop, PConst.FLAG_NONE);
                    else
                        tbl_HocSinhTableAdapter.FillByLopCuAll(qLTH.tbl_HocSinh, idNamHoc, idLop);
                }
            }
            else
            {
                // select all
                if (tscb_Flag_HS.SelectedIndex != -1)
                {
                    if (tscb_Flag_HS.SelectedIndex != tscb_Flag_HS.Items.Count - 1)
                        this.tbl_HocSinhTableAdapter.FillByFlag(this.qLTH.tbl_HocSinh, PConst.HS_FLAG[tscb_Flag_HS.SelectedIndex]);
                    else
                        this.tbl_HocSinhTableAdapter.Fill(this.qLTH.tbl_HocSinh);
                }
                else
                    this.tbl_HocSinhTableAdapter.FillByFlag(this.qLTH.tbl_HocSinh, PConst.FLAG_NONE);
            }
            reloadData();
        }

        private void QuanLyGiaoVien_Resize(object sender, EventArgs e)
        {
            dg_DanhSach.Width = this.Width - 5;
            dg_DanhSach.Height = stt_Bottom.Top - dg_DanhSach.Top - 1;
        }

        private void dg_DanhSach_DataBindingComplete(object sender, EventArgs e)
        {
            reloadData();
        }

        private void reloadData()
        {
            for (int i = 0; i < dg_DanhSach.RowCount; i++)
            {
                dg_DanhSach.Rows[i].Cells[cl_MaGV.Name].Value = PublicLib.formatHSId(
                    int.Parse(dg_DanhSach.Rows[i].Cells[grc_id.Name].Value.ToString()));

                String tmp = "Nam";
                if (((int)dg_DanhSach.Rows[i].Cells[grc_GioiTinh_DB.Name].Value) == 1)
                    tmp = "Nữ";

                dg_DanhSach.Rows[i].Cells[grc_GioiTinh_Disp.Name].Value = tmp;
                dg_DanhSach.Rows[i].DefaultCellStyle.BackColor = (i % 2 == 1) ? PConst.COLOR_BACK_1 : PConst.COLOR_BACK_DEF;
            }
        }

        private void dg_DanhSach_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == cl_MaGV.Index)
            {
                switch (cl_MaGV.HeaderCell.SortGlyphDirection)
                {
                    case SortOrder.None:
                    case SortOrder.Descending:
                        dg_DanhSach.Sort(grc_id, ListSortDirection.Ascending);
                        cl_MaGV.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
                        break;

                    case SortOrder.Ascending:
                        dg_DanhSach.Sort(grc_id, ListSortDirection.Descending);
                        cl_MaGV.HeaderCell.SortGlyphDirection = SortOrder.Descending;
                        break;
                }
            }
            else
            {
                cl_MaGV.HeaderCell.SortGlyphDirection = SortOrder.None;
                if (e.ColumnIndex == grc_GioiTinh_Disp.Index)
                {
                    switch (grc_GioiTinh_Disp.HeaderCell.SortGlyphDirection)
                    {
                        case SortOrder.None:
                        case SortOrder.Descending:
                            dg_DanhSach.Sort(grc_GioiTinh_DB, ListSortDirection.Ascending);
                            grc_GioiTinh_Disp.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
                            break;

                        case SortOrder.Ascending:
                            dg_DanhSach.Sort(grc_GioiTinh_DB, ListSortDirection.Descending);
                            grc_GioiTinh_Disp.HeaderCell.SortGlyphDirection = SortOrder.Descending;
                            break;
                    }
                }
                else
                    grc_GioiTinh_Disp.HeaderCell.SortGlyphDirection = SortOrder.None;
            }
        }

        private void frm_ThemHS_Disposed(object sender, EventArgs e)
        {
            refreshData();
        }

        private void dg_DanhSach_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            tsb_Edit_Click(tsb_Edit, new EventArgs());
        }

        private void deleteHS(int _idHS, int _flag)
        {
            PublicLib.updateField(qLTH.tbl_HocSinh.TableName, qLTH.tbl_HocSinh.idColumn.ColumnName, _idHS.ToString(),
                                  qLTH.tbl_HocSinh.FlagColumn.ColumnName, _flag.ToString());
        }

        private void dg_DanhSach_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            if (MessageBox.Show(MSG.MSG_DELETE, "", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                for (int i = 0; i < dg_DanhSach.SelectedRows.Count; i++)
                {
                    deleteHS((int)dg_DanhSach.SelectedRows[i].Cells[grc_id.Name].Value, 
                             PConst.FLAG_DELETE);
                }
            }
            else
                e.Cancel = true;
        }

        private void tsb_TaoMoi_Click(object sender, EventArgs e)
        {
            AddEditHocSinh frm_ThemHS = new AddEditHocSinh();
            frm_ThemHS.MdiParent = this.MdiParent;
            frm_ThemHS.Show();
            frm_ThemHS.Disposed += new EventHandler(frm_ThemHS_Disposed);
        }

        private void tsb_Edit_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dg_DanhSach.SelectedRows.Count; i++)
            {
                AddEditHocSinh frm_EditHS = new AddEditHocSinh();
                frm_EditHS.Show();
                frm_EditHS.MdiParent = this.MdiParent;
                frm_EditHS.setEditInfo((int)dg_DanhSach.SelectedRows[i].Cells[grc_id.Name].Value);
                frm_EditHS.Disposed += new EventHandler(frm_ThemHS_Disposed);
            }
        }

        private void tsb_Delete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(MSG.MSG_DELETE, "", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                for (int i = 0; i < dg_DanhSach.SelectedRows.Count; i++)
                {
                    deleteHS((int)dg_DanhSach.SelectedRows[i].Cells[grc_id.Name].Value, 
                             PConst.FLAG_DELETE);
                }
                refreshData();
            }
        }

        /*private void tscb_Lop_SelectedIndexChanged(object sender, EventArgs e)
        {
            System.Data.DataView dv = qLTH.tbl_HocSinh.DefaultView;
            
            if (tscb_Lop.SelectedIndex == -1 || tscb_Lop.SelectedIndex == tscb_Lop.Items.Count - 1)
            {
                dv.RowFilter = "";
            }
            else
            {
                dv.RowFilter = "(" + qLTH.tbl_HocSinh.idLopColumn + "=" + 
                    qLTH.tbl_LopHoc.Rows[tscb_Lop.SelectedIndex][qLTH.tbl_LopHoc.idColumn].ToString() + ")";
            }

            dg_DanhSach.DataSource = dv.ToTable();
        }*/

        /*public void sortByLop()
        {
            dg_DanhSach.Sort(grc_idLop, ListSortDirection.Ascending);
            grc_TenLop.HeaderCell.SortGlyphDirection = SortOrder.Ascending;
        }*/

        private void tsb_HocBa_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dg_DanhSach.SelectedRows.Count; i++)
            {
                HocBa frm_HocBa = new HocBa((int)dg_DanhSach.SelectedRows[i].Cells[grc_id.Name].Value);
                frm_HocBa.MdiParent = this.MdiParent;
                frm_HocBa.Show();
            }
        }

        private void tscb_NamHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            refreshData();
        }

        private void tsb_ChuyenLop_Click(object sender, EventArgs e)
        {
            frm_ChonLop = new QuanLyTruongHoc_App.MyControls.ChonLop();
            //frm_ChonLop.MdiParent = this.MdiParent;
            frm_ChonLop.ShowDialog();
            
            frm_ChonLop.OkClicked += new QuanLyTruongHoc_App.MyControls.ChonLop.EventUpdate(frm_ChonLop_OkClicked);
        }

        private void frm_ChonLop_OkClicked(object sender, int _id)
        {
            if (_id == -1)
                return;

            if (MessageBox.Show(MSG.MSG_CHUYEN_LOP, "", MessageBoxButtons.YesNoCancel)
                 == DialogResult.Yes)
            {
                qh_LopHocSinhTableAdapter lopAdap = new qh_LopHocSinhTableAdapter();
                lopAdap.Connection.ConnectionString = PublicLib.getStrConn();

                int idNamHoc = (int)qLTH.tbl_NamHoc[tscb_NamHoc.SelectedIndex][qLTH.tbl_NamHoc.idColumn];

                if (tscb_Lop.SelectedIndex != -1 && tscb_Lop.SelectedIndex < tscb_Lop.Items.Count - 1)
                {
                    // xem cac nam truoc
                    int idLop = (int)qLTH.tbl_LopHoc[tscb_Lop.SelectedIndex][qLTH.tbl_LopHoc.idColumn];
                    lopAdap.FillByNamHocLop(qLTH.qh_LopHocSinh, idNamHoc, idLop);
                }
                else
                {
                    // select all
                    lopAdap.FillByNamHoc(qLTH.qh_LopHocSinh, idNamHoc);
                }

                for (int i = 0; i < dg_DanhSach.SelectedRows.Count; i++)
                {
                    int idHS = (int)dg_DanhSach.SelectedRows[i].Cells[grc_id.Name].Value;

                    DataRow[] tmpRow = qLTH.qh_LopHocSinh.Select(qLTH.qh_LopHocSinh.idHocSinhColumn.ColumnName
                                                                 + "=" + idHS);

                    if (tmpRow.Length > 0)
                    {
                        tmpRow[0][qLTH.qh_LopHocSinh.idLopColumn] = _id;
                        //lopAdap.Update(tmpRow[0]);
                    }
                    else
                    {
                        DataRow nRow = qLTH.qh_LopHocSinh.NewRow();
                        nRow[qLTH.qh_LopHocSinh.idHocSinhColumn] = idHS;
                        nRow[qLTH.qh_LopHocSinh.idNamHocColumn] = idNamHoc;
                        nRow[qLTH.qh_LopHocSinh.idLopColumn] = _id;

                        qLTH.qh_LopHocSinh.Rows.Add(nRow);
                        //lopAdap.Update(nRow);
                    }
                }
                lopAdap.Update(qLTH.qh_LopHocSinh);
                refreshData();
            }
        }

        private void QLHocSinh_Activated(object sender, EventArgs e)
        {
            if (frm_ChonLop != null)
                frm_ChonLop.Focus();

            checkUserPower();
        }

        private void changeFlag(short _flag)
        {
            for (int i=0; i<dg_DanhSach.SelectedRows.Count; i++) 
            {
                int id = (int)dg_DanhSach.SelectedRows[i].Cells[grc_id.Name].Value;
                DataRow[] tmpRow = qLTH.tbl_HocSinh.Select(qLTH.tbl_HocSinh.idColumn.ColumnName + "=" + id);
                if (tmpRow.Length > 0)
                    tmpRow[0][qLTH.tbl_HocSinh.FlagColumn] = _flag;
            }

            tbl_HocSinhTableAdapter.Update(qLTH.tbl_HocSinh);
        }

        private void tsb_ChuyenTruong_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(MSG.MSG_CHUYEN_TRUONG, "", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
                changeFlag(PConst.FLAG_CHUYEN_TRUONG);
        }

        private void mn_DinhChiHoc_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(MSG.MSG_DINH_CHI, "", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
                changeFlag(PConst.FLAG_DINH_CHI);
        }

        private void mn_DuoiHoc_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(MSG.MSG_DUOI_HOC, "", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
                changeFlag(PConst.FLAG_DUOI_HOC);
        }

        private void mnu_tiepTucHoc_Click(object sender, EventArgs e)
        {
            MyDataSet.QLTH.qh_LopHocSinhDataTable lop = new QLTH.qh_LopHocSinhDataTable();
            qh_LopHocSinhTableAdapter adap = new qh_LopHocSinhTableAdapter();
            adap.Connection.ConnectionString = PublicLib.getStrConn();

            int idNamHoc = (int)qLTH.tbl_NamHoc[qLTH.tbl_NamHoc.Rows.Count - 1][qLTH.tbl_NamHoc.idColumn];
            for (int i=0; i<dg_DanhSach.SelectedRows.Count; i++) 
            {
                int idHS = (int)dg_DanhSach.SelectedRows[i].Cells[grc_id.Name].Value;
                adap.FillByNamHocHocSinh(lop, idNamHoc, idHS);

                if (lop.Rows.Count > 0)
                {
                    // hien tai da co lop
                    DataRow[] tmpRow = qLTH.tbl_HocSinh.Select(qLTH.tbl_HocSinh.idColumn.ColumnName + "=" + idHS);
                    if (tmpRow.Length > 0)
                        tmpRow[0][qLTH.tbl_HocSinh.FlagColumn] = PConst.FLAG_NONE;
                }
                else
                {
                    DataRow[] tmpRow = qLTH.tbl_HocSinh.Select(qLTH.tbl_HocSinh.idColumn.ColumnName + "=" + idHS);
                    if (tmpRow.Length > 0)
                        tmpRow[0][qLTH.tbl_HocSinh.FlagColumn] = PConst.FLAG_CHUA_XEP_LOP;
                }
            }

            tbl_HocSinhTableAdapter.Update(qLTH.tbl_HocSinh);
                
        }

        private void tsb_In_Click(object sender, EventArgs e)
        {
            /*Report.DanhSachHocSinh ds = new QuanLyTruongHoc_App.Report.DanhSachHocSinh();
            PublicLib.getThongTinTable(qLTH.tbl_ThongTinNhaTruong); // decrypt data

            PublicLib.fillSysTable(qLTH);

            ds.SetDataSource(qLTH);

            DisplayReport rep = new DisplayReport();
            rep.ShowReport(ds);

            rep.MdiParent = this.MdiParent;
            rep.Show();*/
            PrintDataGrid.PrintDGV print = new PrintDataGrid.PrintDGV();
            print.Print_DataGridView(dg_DanhSach);
        }

    }
}
